package com.zxbg.dao;

import com.zxbg.domain.Approval;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ApprovalDao {
    @Insert("insert into approval values(null, null, ${tenant_id}, null, #{node_handler}, #{process_code}, #{process_content_id}, null, null, null, 0, null)")
    public int addNewApproval(Approval approval);

    @Select("select id from approval order by id desc limit 1")
    public int getNewestId();

    @Update("update approval set process_id = #{process_id} where id = #{id}")
    public int setApprovalProcessIdById(@Param("id") Integer id, @Param("process_id") Integer process_id);

    @Select("select * from approval where process_id =#{process_id} order by id")
    public List<Approval> getTargetApprovalByProcessId(Integer process_id);

    @Update("update approval set if_systemtools = #{if_systemtools}, handler = #{handler}, operate = #{operate}, operate_time = NOW(), if_approved = 1 where id = #{id}")
    public int setOperate(Approval approval);

    @Select("select * from approval where process_id = #{process_id} and process_content_id = #{process_content_id} and if_approved = 0")
    public List<Approval> getNotPassNode(@Param("process_id") Integer process_id, @Param("process_content_id") Integer process_content_id);

    @Update("update approval set if_approved = 1 where id = #{id}")
    public int setNodeApproved(Integer id);

    @Update("update approval set if_approved = 1, operate = 'Reject', operate_time = NOW() where id = #{id}")
    public int setNodeReject(Integer id);

    @Select("select * from approval where handler = #{handler} and tenant_id = #{tenant_id}")
    public List<Approval> getAllMyDone(@Param("handler") String handler, @Param("tenant_id") Integer tenant_id);

    @Update("update approval set if_systemtools = 1, if_approved = 1, operate = #{operate}, handler = #{handler}, operate_time = NOW(), operate_content = #{operate_content} where id = #{id}")
    public int setNodeOtherOperate(Approval approval);

    @Update("update approval set if_approved = 1 where process_id = #{process_id}")
    public int setLeftNodesApproved(Integer process_id);

    @Update("update approval set if_systemtools = 1, handler = null, operate = null, operate_content = concat(#{operate_content}, ' & ', NOW()), operate_time = null, if_approved = 0 where id = #{id}")
    public int setNodeBack(Approval approval);

    @Update("update approval set if_systemtools = 1, handler = null, operate = null, operate_time = null, if_approved = 0 where id = #{id}")
    public int otherNodesBack(Integer id);
}
